<template>
	<view class="content">
		<view v-for="(item,index) in homeArr" :key="index">
			<view class="homearr">
				<view class="home" @click="home(item)">
					<text>房屋名称：{{item.name}}</text>
					<text>设备数量：{{item.deviceList.length}}</text>
				</view>
				<view>
					<text class="edit" @click="edit(item)">编辑</text>
					<text class="del" @click="del(item)">删除</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				homeArr: []
			}
		},
		onShow: function() {
			this.getHomeList();
		},
		methods: {
			onNavigationBarButtonTap(e) {
				uni.navigateTo({
					url: '/pages/house/addhouse'
				});
			},
			getHomeList() {
				uni.showLoading({
					title: '正在加载房屋列表'
				});
				try {
					const homeModule = uni.requireNativePlugin("LJY-HomeModule") //调用android中自定义的插件名称
					const ret = homeModule.homeList();
					uni.hideLoading(); //加载结束
					console.log(ret);
					if (ret.code == 0) {
						this.homeArr.splice(0, this.homeArr.length); //清空数组
						for (let i = 0; i < ret.data.length; i++) { //添加数组值
							this.homeArr.push(ret.data[i]);
						}
						console.log(this.homeArr)
						//因为HomeList接口返回的没有家庭对应的设备数，所以这里遍历家庭列表去依次获取家庭详情，从家庭详情接口中获取设备列表
						/* for (let ii = 0; ii < this.homeArr.length; ii++){
							//调用家庭详情接口
							const retOfhomeDetail = homeModule.homeDetail({ //login表示自定义插件的函数名，第一个表示要传递的参数
								homeId: this.homeArr[ii].homeId
							});
							if (retOfhomeDetail.code == 0) { //如果调用接口成功
								//获取该家庭的设备列表
								const deviceList = retOfhomeDetail.data.deviceList;
								//为解决视图渲染问题，这里直接遍历设备链表添加
								for (let j = 0; j < deviceList.length; j++) {
									this.homeArr[ii].deviceList.push(deviceList[j]);
								}
							} */
						// }
					} else {
						uni.showToast({
							title: ret.msg,
							duration: 2000
						});
					}
				} catch (e) { //如果涂鸦智能无法连接
					uni.hideLoading(); //加载结束
					uni.showToast({
						title: '涂鸦智能家居暂无法使用，请稍后再试',
						duration: 2000
					});
				}
			},
			//房子设备列表
			home(homeArr) {
				console.log("我们的homeid",homeArr.homeId)
				uni.navigateTo({
					url: '/pages/house/equipment/equipment?name=' + homeArr.name + '&homeId=' + homeArr.homeId
				})
			},
			//删除
			del(homeArr) {
				console.log('删除', homeArr.homeId)
				uni.showToast({
					title: '删除ID--' + homeArr.homeId,
					icon: 'none'
				})
			},
			//编辑
			edit(homeArr) {
				console.log('编辑', homeArr.homeId)
				uni.navigateTo({
					url: '/pages/house/edithouse?homeId=' + homeArr.homeId + '&name=' + homeArr.name
				});
			},
		},
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.homearr {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-width: 1rpx;
		border-bottom-style: solid;
		width: 750rpx;
		height: 100rpx;
		font-size: 30rpx;

		.home {
			align-items: center;
			flex-wrap: wrap;
			width: 480rpx;
			margin-left: 30rpx;

			text {
				width: 100%;
				text-align: left;
			}
		}

		view {
			display: flex;
			width: 280rpx;
			height: 100rpx;
			text-align: center;

			.edit {
				line-height: 100rpx;
				color: #FFFFFF;
				text-align: center;
				width: 140rpx;
				height: 100rpx;
				background: #55aaff;
			}

			.del {
				line-height: 100rpx;
				color: #FFFFFF;
				text-align: center;
				width: 140rpx;
				height: 100rpx;
				background: #ff0000;
			}
		}
	}

	.shouye_header {
		width: 100%;
		background-color: #109ef8;
		text-align: center;
	}

	.shouye_header image {
		float: right;
		z-index: 999;
		margin-top: -70rpx;
		margin-right: 30rpx;
		width: 50rpx;
		height: 50rpx;
	}

	.arrivalNavigation {
		width: 250rpx;
		position: absolute;
		right: 0rpx;
		top: 20rpx;

		// z-index: 99;

		.sideNavigation {
			width: 230rpx;
			background-color: #C0C0C0;

			border-radius: 10rpx;

			view {
				// height: 85rpx;
				text-align: center;
				line-height: 85rpx;
				font-size: 25rpx;
				list-style-type: none;
				border-bottom: 1rpx solid #ebebeb;
			}
		}
	}
</style>
